import pandas as pd
import re
import matplotlib.pyplot as plt

#1.1
data = pd.read_csv("m-a_edges.csv",sep='\t',header=0)
#新建一个字典nums来存储Mashup中的包含Web API个数
nums = {}
for row in range(data.shape[0]):
    if data.values[row][0] in nums:
        nums[data.values[row][0]] += 1
    else:
        nums[data.values[row][0]] = 1
nums = dict(sorted(nums.items(), key = lambda x:x[1], reverse = True))

#1.2
#新建一个字典frequency来存储每个Web API被使用的次数
frequency = {}
for row in range(data.shape[0]):
    if data.values[row][1] in frequency:
        frequency[data.values[row][1]] += 1
    else:
        frequency[data.values[row][1]] = 1
frequency = dict(sorted(frequency.items(), key = lambda x:x[1], reverse = True))
#print(frequency)

#1.3
number = {}
for i in range(1,6):
    route = 'raw/accessibility/api_accessibility/api_version_accessbiliby-{}.txt'.format(i)
    f = open(route)
    data2 = f.read().splitlines()
    f.close()
    for row in data2:
        Co = re.search(r'(?<=[/ | \.])\w*?(?=\.(com|gov|edu|mil|in|org|uk|de|net|usa|space|io)/)', row)
        if Co != None and Co.group() != 'programmableweb':
            if Co.group() in number:
                number[Co.group()] += 1
            else:
                number[Co.group()] = 1
number = dict(sorted(number.items(), key = lambda x:x[1], reverse = True))


#第二题
data3 = pd.read_csv('mashup_nodes_estimator.csv', sep='\t', header=0)
M_C = {}
for row in range(data3.shape[0]):
    M_C[data3.values[row][2]] = data3.values[row][6]

data4 = pd.read_csv('api_nodes_estimator.csv', sep='\t', header=0)
A_C = {}
for row in range(data4.shape[0]):
    A_C[data4.values[row][1]] = data4.values[row][6]


Category = {}
for row in range(data.shape[0]):
    mashupName = data.values[row][0]
    apiName = data.values[row][1]
    masC = M_C[mashupName]
    if M_C[mashupName] not in Category:
        if apiName in A_C:
            apiC = A_C[apiName]
            Category.setdefault(masC, {})[apiC] = 1
    else:
        if apiName in A_C:
            apiC = A_C[apiName]
            if apiC in Category[masC]:
                Category[masC][apiC] += 1
            else:
                Category[masC][apiC] = 1

#绘制饼状图
labels=['Social','eCommerce','Advertising','Mapping','Search','Blogging','Cloud']
X=[4,6,3,3,3,1,1]
fig = plt.figure()
plt.pie(X,labels=labels,autopct='%1.2f%%')
plt.title("Pie chart")
plt.show()



#第三题
f2 = open('raw/all_pairs.txt',encoding='UTF-8')
data5 = f2.readlines()
f2.close()
C_P = {} #用于存放每种Category的全部percent
res = {} #存放最终的加权结果
for row in data5[1:]:
    List = row[1:-3].split(',')
    L = List[0].strip('\'')
    R = List[1][1:].strip('\'')
    percent = float(List[5].strip())
    if L in A_C and R in A_C and A_C[L] == A_C[R]:
        C_P.setdefault(A_C[L], [])
        C_P[A_C[L]].append(percent)
for key, value in C_P.items():
    sumpercent = 0
    for item in value:
        sumpercent += item
    res[key] = sumpercent / len(value)
res = dict(sorted(res.items(), key = lambda x:x[1], reverse = True))

#绘制柱形图
fig = plt.figure()
x=list(res.keys())
x=list(map(str,x))
y=list(res.values())
plt.subplot(212)
plt.bar(x,y)
plt.xticks (fontsize=7,rotation = 90)
plt.xlabel('category')
plt.ylabel('percent')
plt.title('Graph')
plt.show()